<?php

/**
 * ring_news actions.
 *
 * @package    sf_sandbox
 * @subpackage ring_news
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class ring_newsActions extends autoring_newsActions
{
 public function executeIndex()
  {
    return $this->forward('ring_news', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();


    // add
    
    $con = new MysqlDAL('myblog');
    $sql = "SELECT * FROM irring_posts 
    		WHERE post_status = 'publish'   
        	ORDER BY ID DESC";
    $res = $con->doSelect($sql);
    $this->pager = $res;


    $con = new MysqlDAL('propel');
    $sql = "Select id from iring_news";
    $res = $con->doSelect($sql);
    $this->currentNews = $res;
    
    // pager
    /*$this->pager = new sfPropelPager('IringNews', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/iring_news')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/iring_news');
    }*/
    
  }
  public function executeSave()
  {
  	$cid = $this->getRequestParameter('cid');
  	$order = $this->getRequestParameter('order');
  	
  	$con = new MysqlDAL('myblog');
    $sql = "SELECT id,post_date,post_title,post_content,guid
    		FROM irring_posts 
    		WHERE id IN (".implode(",",$cid).")";
    $res = $con->doSelect($sql);
    
   	IringNewsPeer::doDeleteAll();
   	
   	
    foreach($res as $item){
    	#$iring_news = $this->getIringNewsOrCreate();
    	$iring_news = new IringNews();
    	$content = substr(strip_tags($item['post_content']) , 0 ,200);
    	$content = substr($content,0, strrpos($content," ")).'...';
    	
    	
    	$iring_news->setId($item['id']);
    	$iring_news->setTitle($item['post_title']);
    	$iring_news->setCreatedDatetime($item['post_date']);
    	$iring_news->setContent($content);
    	$iring_news->setSort($order[$item['id']]);
    	$iring_news->setUrl($item['guid']);
    	$iring_news->save();
    }
    return $this->redirect('ring_news/list');
  }
  
}
